package com.itbaizhan.openhis.service.impl;

import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.domain.Income;
import com.itbaizhan.openhis.domain.Refund;
import com.itbaizhan.openhis.dto.RevenueQueryDto;
import com.itbaizhan.openhis.mapper.RevenueMapper;
import com.itbaizhan.openhis.service.RevenueService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RevenueServiceImpl implements RevenueService {

    @Autowired
    private RevenueMapper revenueMapper;

    @Override
    public Map<String, Object> queryAllRevenueData(RevenueQueryDto revenueQueryDto) {
        //定义一个总的map,下面根据收支统计，收支概况，收入渠道，退款，各定义一个map
        //最后全部放进这总map，返回前端
        HashMap<String,Object> map = new HashMap<>();

        List<Income> incomes = revenueMapper.queryIncome(revenueQueryDto);//收入

        List<Refund> refunds = revenueMapper.queryRefund(revenueQueryDto);//退费

        //声明需要的变量
        Double totalRevenue = 0.0; //合计收入
        Double toll = 0.0; //总收入
        Double refund= 0.0; //总退费
        Double cashIncome =0.0; //现金支付
        Double alipayIncome = 0.0; //支付宝支付
        Double cashRefund = 0.0; //现金退费
        Double alipayRefund = 0.0;//支付宝退费
        Integer incomeChannelCash = 0; //现金收费次数
        Integer incomeChannelAlilpay = 0; //支付宝收费次数

        //循环收费，判断收费是支付宝还是现金收费
        for(Income income: incomes){
            toll +=income.getOrderAmount();
            if(income.getPayType().equals(Constants.PAY_TYPE_0)){
                //现金支付
                cashIncome +=income.getOrderAmount();
                incomeChannelCash++;
            }else {
                //支付宝支付
                alipayIncome +=income.getOrderAmount();
                incomeChannelAlilpay++;
            }
            for (Refund refund1 : refunds) {
                refund +=refund1.getBackAmount();
                if(refund1.getBackType().equals(Constants.PAY_TYPE_0)){
                    cashRefund +=refund1.getBackAmount();
                }else {
                    alipayRefund +=refund1.getBackAmount();
                }
            }
        }
        //收支统计
        HashMap<String,Object> revenueObject = new HashMap<>();
        totalRevenue = toll-refund;
        revenueObject.put("totalRevenue",totalRevenue);

        HashMap<String,Object> overViewMap = new HashMap<>();
        overViewMap.put("toll",toll);
        overViewMap.put("refund",refund);

        HashMap<String,Object> channelMap = new HashMap<>();
        channelMap.put("cashIncome",cashIncome);
        channelMap.put("alipayIncome",alipayIncome);
        channelMap.put("cashRefund",cashRefund);
        channelMap.put("alipayRefund",alipayRefund);

        revenueObject.put("totalRevenue",totalRevenue);
        revenueObject.put("overView",overViewMap);
        revenueObject.put("channel",channelMap);
        //封装到总的map
        map.put("revenueObject", revenueObject);

        //收支概况,先创建用来总封装的map(revenueOverView ) ,再创建ArrayList，map集合revenueOverViewData1与revenueOverViewData1
        //把两个集合添加到list,最后存放到map(revenueOverView )
        HashMap<String,Object> revenueOverView = new HashMap<>();
        revenueOverView.put("title","收支概况");
        ArrayList<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> revenueOverViewData1 = new HashMap<>();
        revenueOverViewData1.put("name","收费金额");
        revenueOverViewData1.put("value",toll);
        Map<String,Object> revenueOverViewData2 = new HashMap<>();
        revenueOverViewData2.put("name","退费金额");
        revenueOverViewData2.put("value",refund);
        list.add(revenueOverViewData1);
        list.add(revenueOverViewData2);
        revenueOverView.put("data",list);

        map.put("revenueOverView",revenueOverView);

        //收入渠道
        HashMap<String,Object> incomeChannel = new HashMap<>();
        incomeChannel.put("title","收费渠道");
        ArrayList<Map<String,Object>> list1 = new ArrayList<>();
        Map<String,Object> revenueOverViewData3 = new HashMap<>();
        revenueOverViewData3.put("name","现金收入");
        revenueOverViewData3.put("value",incomeChannelCash);
        Map<String,Object> revenueOverViewData4 = new HashMap<>();
        revenueOverViewData4.put("name","支付宝收入");
        revenueOverViewData4.put("value",incomeChannelAlilpay);
        list1.add(revenueOverViewData3);
        list1.add(revenueOverViewData4);
        incomeChannel.put("data",list1);

        map.put("incomeChannel",incomeChannel);
        //退款
        HashMap<String,Object> refundMap = new HashMap<>();
        refundMap.put("title","退款");
        ArrayList<Map<String,Object>> list2 = new ArrayList<>();
        Map<String,Object> revenueOverViewData5 = new HashMap<>();
        revenueOverViewData5.put("name","现金退费");
        revenueOverViewData5.put("value",cashRefund);

        Map<String,Object> revenueOverViewData6 = new HashMap<>();
        revenueOverViewData6.put("name","支付宝退费");
        revenueOverViewData6.put("value",alipayRefund);

        list2.add(revenueOverViewData5);
        list2.add(revenueOverViewData6);

        refundMap.put("data",list2);

        map.put("refund",refundMap);
        return map;
    }
}
